// https://mp.weixin.qq.com/s/P4KN2hBtV-FBLsssImBFpw

#include <iostream>
#include <memory>

typedef std::shared_ptr<int> IntPtr;


void Display(IntPtr v)
{
    printf(":---> %d\n", v.use_count());
    IntPtr t = v;
    printf(":---> %d\n", v.use_count());
    printf(":---> %d\n", t.use_count());
}

main(int argc, char const *argv[])
{
    std::shared_ptr<int> p = std::make_shared<int>();
    *p = 78;
    Display(p);
    std::cout << "p = " << *p << "\n";
    std::cout << "p reference count =  " << p.use_count() << "\n";

    {
        std::shared_ptr<int> p1(p);
        std::cout << "p1 = " << *p1 << "\n";
        std::cout << "p reference count =  " << p.use_count() << "\n";
        std::cout << "p1 reference count =  " << p1.use_count() << "\n";
    }
    std::cout << "-----------------------1\n";
    std::cout << "p reference count =  " << p.use_count() << "\n";
    std::shared_ptr<int> p1 = p;
    *p1 = 10;
    std::cout << "-----------------------2\n";
    std::cout << "p reference count =  " << p.use_count() << "\n";
    std::cout << "p1 reference count =  " << p1.use_count() << "\n";
    // p1.reset();
    std::cout << "-----------------------3\n";
    p1 = nullptr;
    std::cout << "p reference count =  " << p.use_count() << "\n";
    std::cout << "p1 reference count =  " << p1.use_count() << "\n";
    p1.reset(new int(11));
    std::cout << "p1 = " << *p1 << "\n";
    std::cout << "p = " << *p << "\n";
    std::cout << "p reference count =  " << p.use_count() << "\n";
    std::cout << "p1 reference count =  " << p1.use_count() << "\n";
    /* code */
    return 0;
}
